/**
 * 
 */
package com.zc.spring.serviceimpl;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

import com.zc.spring.util.Formate;

/**
 * @author ZhangCheng
 * @time 2019年11月1日
 */
@Component
public class AopExampleHandler {
	public void printNowWorkingTime() {
		System.out.println("后置通知 "+this.getClass().getSimpleName()+" Says Now："+ Formate.getNowTime());
	}
	public void doException(JoinPoint j,Exception e) {
		System.out.println("异常！信息是："+e.getMessage());
	}
	public void doBefore() {
		System.out.println("前置通知"+this.getClass().getSimpleName());
	}
	public void doReturning(Object ret) {
		if(ret!=null) {
			System.out.println("返回通知 doReturning...." + ret);
		}else {
			System.out.println("返回通知 doReturning....");
		}
	
	}
	//对于单个的通知来说，around环绕通知，只要开始就会一直执行完后再继续执行其他的
	public Object doAround(ProceedingJoinPoint pJP) throws Throwable {
		Object ret = null;
		String methodName = pJP.getSignature().getName();
		try {
			//前置通知
			System.out.println("This is the Method of "+methodName+" starting... here is params"+Arrays.asList(pJP.getArgs()));
			ret = pJP.proceed();
			System.out.println("This is the returning of "+ methodName+ "end. ret = "+ ret);
		}catch (Throwable e) {
			System.out.println("This is the" + methodName+" exception. Message is "+ e.getMessage());
			throw new Throwable();
		}
		System.out.println("This is the "+methodName + "end");
		return ret;
	}
	
}
